// utils/menuGenerator.ts

interface RouteMeta {
  showInMenu?: boolean;
  title?: string;
  icon?: string;
  parent?: string;
  parentIcon?: string;
}

interface Route {
  path: string;
  meta?: RouteMeta;
}

interface MenuItem {
  name: string;
  path?: string;
  icon: string;
  children?: MenuItem[];
  collapsed?: boolean;
}

export function generateMenuItems(routes: Route[]): MenuItem[] {
  const menu: MenuItem[] = [];
  const parentMap: Record<string, MenuItem> = {};

  routes.forEach(route => {
    const meta = route.meta;
    if (meta?.showInMenu) {
      const menuItem: MenuItem = {
        name: meta.title || '',
        path: route.path,
        icon: meta.icon || 'fas fa-circle',
        children: []
      };

      if (meta.parent) {
        // 处理子菜单
        if (!parentMap[meta.parent]) {
          parentMap[meta.parent] = {
            name: meta.parent,
            icon: meta.parentIcon || 'fas fa-folder',
            children: [],
            collapsed: true
          };
          menu.push(parentMap[meta.parent]);
        }
        parentMap[meta.parent].children?.push(menuItem);
      } else {
        // 处理顶级菜单
        menu.push(menuItem);
      }
    }
  });

  return menu;
}
